package com.xiong.wuliao.HomeGame;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;

import com.xiong.wuliao.R;

public class StartActivity extends AppCompatActivity {
    private SharedPreferences sPreferences;
    private ImageButton myButtonGameBack; // 返回按钮

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start);
        initView();
        sPreferences = getSharedPreferences("game.xml", Context.MODE_PRIVATE);
    }

    private void initView() {
        myButtonGameBack = (ImageButton) findViewById(R.id.myButtonGameBack);
        myButtonGameBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StartActivity.this.finish();
            }
        });
    }

    public void onSwitch(View view) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("音乐提醒");
        String[] items = {"游戏音效", "背景音乐"};
        boolean[] checkedItems = {sPreferences.getBoolean("effectMusic", false), sPreferences.getBoolean("bgMusic", false)};
        builder.setMultiChoiceItems(items, checkedItems, new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                switch (which) {
                    case 0:
                        sPreferences.edit().putBoolean("effectMusic", isChecked).commit();
                        break;
                    case 1:
                        sPreferences.edit().putBoolean("bgMusic", isChecked).commit();
                        break;
                    default:
                        break;
                }
            }
        });
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });
        // 外部不可被取消点击
        builder.setCancelable(false);
        builder.show();
    }

    public void onRank(View view) {
        PlayerListDialog dialog = new PlayerListDialog(StartActivity.this);
        dialog.show();
    }

    public void onStart(View view) {
        Intent intent = new Intent(StartActivity.this, GameActivity.class);
        startActivity(intent);
    }
}
